package io.renren.modules.weldingmachine.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.renren.modules.welderinfo.service.impl.WelderinfoServiceImpl;
import io.renren.modules.weldingmachine.entity.AnalysisInfoResponse;
import io.renren.modules.weldingmachine.entity.UserateInfoResponse;
//import io.renren.modules.weldingmachine.entity.UserateResponse;
import io.renren.modules.weldingmachine.entity.WarnInfoResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.weldingmachine.dao.WeldingmachineDao;
import io.renren.modules.weldingmachine.entity.WeldingmachineEntity;
import io.renren.modules.weldingmachine.service.WeldingmachineService;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;


@Service("weldingmachineService")
public class WeldingmachineServiceImpl extends ServiceImpl<WeldingmachineDao, WeldingmachineEntity> implements WeldingmachineService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WeldingmachineEntity> page = this.page(
                new Query<WeldingmachineEntity>().getPage(params),
                new QueryWrapper<WeldingmachineEntity>()
        );

        return new PageUtils(page);
    }


    //1.获取token
    public Object getWarnToken() throws JsonProcessingException {
        RestTemplate restTemplate = new RestTemplate();

        // 获取token
//        UsersServiceImpl.ReHeader reHeader = new UsersServiceImpl.ReHeader();
        WelderinfoServiceImpl.ReHeader reHeader = new WelderinfoServiceImpl.ReHeader();
        reHeader.setKey("cmhi2023");
        String url = "http://10.142.3.129:8080/getOpenApiToken";
        String result = restTemplate.postForObject(url,reHeader, String.class);
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(result);
        String token = String.valueOf(jsonNode.get("data").get("token")).replace("\"","");
        return token.toString();
    }

    @Autowired
    private RestTemplate restTemplate;
//    @Scheduled(cron = "0/5 * * * * ?")
    public List<WarnInfoResponse.WarnInfo> getWarnInfo(Optional<Long> warninfoId, Optional<String> warnCode, Optional<String> warnComment, Optional<String> warnPa, Optional<String> warnPv, Optional<Integer> wmId, Optional<String> wmNo, int pageSize, int pageNum) throws JsonProcessingException, UnsupportedEncodingException {
//        int pageSize = 20;
//        int pageNum = 1;
        UriComponentsBuilder builder = UriComponentsBuilder
                .fromUriString("http://10.142.3.129:8080/system/warninfo/list")
                .queryParam("warninfoId", warninfoId)
                .queryParam("warnCode", warnCode)
                .queryParam("warnComment", warnComment)
//                .queryParam("warnTime", warnTime)
                .queryParam("warnPa", warnPa)
                .queryParam("warnPv", warnPv)
                .queryParam("wmId", wmId)
                .queryParam("wmNo", wmNo)
                .queryParam("pageSize", pageSize)
                .queryParam("pageNum", pageNum);

        String token=getWarnToken().toString();
        System.out.println(token);
        HttpHeaders headers = new HttpHeaders();
        headers.setBearerAuth(token);
        HttpEntity<String> entity = new HttpEntity<>("parameters", headers);

//        ResponseEntity<List<WarnInfo>> response = restTemplate.exchange(
        ResponseEntity<WarnInfoResponse> response = restTemplate.exchange(
                builder.toUriString(),
                HttpMethod.GET,
                entity,
                WarnInfoResponse.class
        );
        System.out.println(response);
        System.out.println(response.getBody().getRows());
        System.out.println("Response status: " + response.getStatusCode());
        System.out.println("Response body: " + response.getBody());
        System.out.println("----------报警信息=======");

        return response.getBody().getRows();
    }


    public List<UserateInfoResponse.UserateInfo> getUserate(String startDate, String endDate, String silkDiameter, String silkMeltrate, Integer theoWorkTime, String pauseRate, String gwsn) throws JsonProcessingException, UnsupportedEncodingException {
//        int pageSize = 20;
//        int pageNum = 1;
        UriComponentsBuilder builder = UriComponentsBuilder
                .fromUriString("http://10.142.3.129:8080/system/userate/list")
                .queryParam("startDate", startDate)
                .queryParam("endDate", endDate)
                .queryParam("silkDiameter", silkDiameter)
//                .queryParam("warnTime", warnTime)
                .queryParam("silkMeltrate", silkMeltrate)
                .queryParam("theoWorkTime", theoWorkTime)
                .queryParam("pauseRate", pauseRate)
                .queryParam("gwsn", gwsn);
//        System.out.println(builder);

        String token=getWarnToken().toString();
        System.out.println(token);
        HttpHeaders headers = new HttpHeaders();
        headers.setBearerAuth(token);
        HttpEntity<String> entity = new HttpEntity<>("parameters", headers);

        System.out.println(entity);
//        ResponseEntity<List<WarnInfo>> response = restTemplate.exchange(
        ResponseEntity<UserateInfoResponse> response = restTemplate.exchange(
                builder.toUriString(),
                HttpMethod.GET,
                entity,
                UserateInfoResponse.class
        );
        System.out.println(response);
        System.out.println(response.getBody().getData());
        System.out.println(response);
        System.out.println("Response status: " + response.getStatusCode());
        System.out.println("Response body: " + response.getBody());
        System.out.println("----------利用率信息=======");

        return response.getBody().getData();
    }

    public List<AnalysisInfoResponse.AnalysisInfo> getAnalysisInfo(String startDate, String endDate, String pauseRate, int pageSize, int pageNum,Optional<String> gwsn) throws JsonProcessingException, UnsupportedEncodingException {
//        int pageSize = 20;
//        int pageNum = 1;
        UriComponentsBuilder builder = UriComponentsBuilder
                .fromUriString("http://10.142.3.129:8080/system/analysis/list")
                .queryParam("startDate", startDate)
                .queryParam("endDate", endDate)
                .queryParam("pauseRate", pauseRate)
                .queryParam("pageSize", pageSize)
                .queryParam("pageNum", pageNum)
                .queryParam("gwsn", gwsn);

        String token=getWarnToken().toString();
        System.out.println(token);
        HttpHeaders headers = new HttpHeaders();
        headers.setBearerAuth(token);
        HttpEntity<String> entity = new HttpEntity<>("parameters", headers);

//        ResponseEntity<List<WarnInfo>> response = restTemplate.exchange(
        ResponseEntity<AnalysisInfoResponse> response = restTemplate.exchange(
                builder.toUriString(),
                HttpMethod.GET,
                entity,
                AnalysisInfoResponse.class
        );
        System.out.println(response);
        System.out.println(response.getBody().getRows());
        System.out.println("Response status: " + response.getStatusCode());
        System.out.println("Response body: " + response.getBody());
        System.out.println("----------数据分析=======");

        return response.getBody().getRows();
    }

}